// (c) 2008 Kickstone Technology Ltd

#include "stdafx.h"
#include "ButtonDemoBHO.h"
#include "IEWindow.h"
#include "ButtonDemoButton.h"


HRESULT CButtonDemoButton::FinalConstruct(){
	return S_OK;
}

void CButtonDemoButton::FinalRelease(){
}

///////////////////////////////////////////////////////////////////////////
// IObjectWithSite function
///////////////////////////////////////////////////////////////////////////
STDMETHODIMP CButtonDemoButton::SetSite(IUnknown *pUnkSite) {
	// Called by IE just after the BHO created
	if (!pUnkSite)
		ATLTRACE("\nCButtonDemoButton::SetSite(): pUnkSite is NULL\n\n");
	else {
		// Query pUnkSite for the IWebBrowser2 interface.
		CComQIPtr<IServiceProvider> spSP=pUnkSite;
		if(spSP){
			HRESULT hr=spSP->QueryService(IID_IWebBrowserApp, &m_spWebBrowser2);
			if (SUCCEEDED(hr) && m_spWebBrowser2){
				getHWND();
				m_ieWindow=CIEWindow::findWindow(m_hWnd);
			}
		}
	}
	return S_OK;
}

///////////////////////////////////////////////////////////////////////////
// IOleCommandTarger functions
///////////////////////////////////////////////////////////////////////////
STDMETHODIMP CButtonDemoButton::QueryStatus(const GUID* pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT* pCmdText){
	if (!cCmds)
		return E_INVALIDARG;
	if (!prgCmds)
		return E_POINTER;

	prgCmds[0].cmdf = OLECMDF_ENABLED;
	return S_OK;
}

STDMETHODIMP CButtonDemoButton::Exec(const GUID*, DWORD nCmdID, DWORD d, VARIANTARG*, VARIANTARG* pvaOut){
	ATLTRACE("CButtonDemoButton::Exec\n");
	if (m_ieWindow){
		CButtonDemoBHO *bho=m_ieWindow->getCurrentBHO();
		if (bho)
			bho->buttonPressed();
	}
	return S_OK;
}

///////////////////////////////////////////////////////////////////////////
// Internal functions
///////////////////////////////////////////////////////////////////////////
void CButtonDemoButton::getHWND(){
	// get the HWND of the main browser window
	SHANDLE_PTR nBrowser = NULL;
	HRESULT hr=m_spWebBrowser2->get_HWND(&nBrowser);
	m_hWnd = (HWND)nBrowser;
}
